package org.penguin.boot.security;

import lombok.extern.slf4j.Slf4j;
import org.passay.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Arrays;
import java.util.List;

/**
 * @author : cyhch
 * @version : 1.0
 * @createdTime : 2023/12/21 21:40
 * @since : 1.0
 */
@Slf4j
public class Study {

    public static void main(String[] args) {
        testPassay();
        testPasswordEncoder();
        testPasswordEncoderWithSalt();
    }

    private static void testPasswordEncoder() {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        log.info("密码校验:" + passwordEncoder.matches("12xy34ZV", "$2a$10$rUiJ8fkTFlK5r1/yCI4VPeGRH5x.BwAtWyrjAxpbgWFOSAF.GiX9C"));
    }

    private static void testPasswordEncoderWithSalt() {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        log.info("密码校验:" + passwordEncoder.matches("12xy34ZV"+ "SJQMa0ZbmaJeiuoM1ko0/g==", "$2a$10$G3IgGjqVTs6nKMCGaoJau.FbKHukdCZbFwl7Key151K7/3q5bCvY6"));
    }

    static void testPassay() {
        PasswordValidator validator = new PasswordValidator(Arrays.asList(new CharacterRule(EnglishCharacterData.UpperCase, 1),
                                                                          new CharacterRule(EnglishCharacterData.LowerCase, 1),
                                                                          new CharacterRule(EnglishCharacterData.Digit, 1),
                                                                          new CharacterRule(EnglishCharacterData.Special, 1),
                                                                          new WhitespaceRule(), new LengthRule(8, 30)));

        RuleResult result = validator.validate(new PasswordData("12345678"));
        List<String> messages = validator.getMessages(result);
        String messageTemplate = String.join(",", messages);
        System.out.println(messageTemplate);
    }
}
